const path = require("path")
const jwt = require('jsonwebtoken');
const koaJwt = require("koa-jwt")
const router = require("koa-Router")()
const db = require("../db/db")
const SECRET = require("../SECRET") // 密钥
console.log(SECRET)
// router.get("/",async (ctx) => {
//     ctx.body = "hello shouye"
// })
// 查找接口
router.get("/list",async (ctx) => {
    let {rows} = await db.query("select * from imgs")
    console.log("12",rows)
    ctx.body = rows
})
// 增加数据
// 上传图片
router.post("/upload",async (ctx) => {
    // img 是传入的图片字段
// console.log(ctx.request.files.img)
const img = ctx.request.files.img
if(img){
    // 上传成功
    console.log(img.filepath)
    ctx.body = {
        status:200,
        code:0,
        message:"图片上传成功",
        data:{
            img_src:path.basename(img.filepath)
        }
    }
}


})
router.post("/add",async (ctx) => {
    console.log(ctx.request.body)
    // post 传递的参数
    let bodys = ctx.request.body 
    console.log(bodys,"38")
    let img_src = bodys.img_src  // 前端图片字段
    let title = bodys.title //标题字段
    let desc = bodys.desc // 详情字段
    let startus = bodys.startus // 状态字段
    let sql = `insert into imgs(img_src,title,desc,startus) values('${img_src}','${title}','${desc}','${startus}')`
    let {rows} = await db.query(sql)
    console.log(rows,"41")
    if(rows){
        ctx.body = {
            status:200,
            data:rows,
            message:"sucess"
        }
    }else{
        ctx.body = {
            status:400,
            message:"添加失败"
        }
    }
    
    
})
// 删除数据
router.delete("/delete",async (ctx) => {
    let query = ctx.request.query
    // query.id 是前端传递过来的id号，识别八张图片的标识
    let sql = `delete from goods where id='${query.id}'`
    let {rows} = await db.query(sql)
    console.log("12",rows)
    if(rows){
        ctx.body = {
            status:200,
            message:"删除成功"
        }
    }else{
        ctx.body = {
            status:400,
            message:"请求失败"
        }
    }
    
})
// 修改数据
router.put("/update",async (ctx) => {
    let bodys = ctx.request.body
    // img_src id 前端字段
    let sql = `update imgs set img_src='${bodys.img_src}' where id = ${bodys.id}`
    let {rows} = await db.query(sql)
    if(rows){
        ctx.body = {
            status:200,
            message:"修改成功"
        }
    }else{
        ctx.body = {
            status:400,
            message:"修改失败"
        }
    }
    // console.log("35",bodys)
    // ctx.body = bodys
})
// 搜索保存
router.post("/save",async (ctx) => {
    let bodys = ctx.request.body
    let name = bodys.name
    console.log(name)
    let sql = `insert into goods  (name) values('${name}')`
    let {rows} = await db.query(sql)
    if(rows) {
        ctx.body = {
            status:200,
            message:"保存成功"
        }
    }else{
        ctx.body = {
            status:400,
            message:"请求失败"
        }
    }
})
// 登录注册
router.post("/login",async (ctx) => {
    let bodys = ctx.request.body
    // ctx.set("Content-Type", "application/json;charset=utf-8")
    console.log(bodys)
    let {username,password} = bodys
    let sql = `select * from users where username='${username}'`
    let {rows} = await db.query(sql)
    console.log("12",rows[0].password,password)
    if(rows.length>0) {
        // 查找到了用户
        if(rows[0].password === bodys.password) {
            let token = jwt.sign({username},SECRET,{ expiresIn: '30d' })
            ctx.body = {
                status:200,
                message:"success",
                // token,
                data:{
                    mess:"登录成功"
                }

            }
        }else{
            ctx.body = {
                status:400,
                message:"error",
                data:{
                    mess:"密码不对"
                }
            }
        }
    }else{
        ctx.body = {
            status:400,
            message:"用户不存在，请先注册"
        }
    }
    
})
// 注册
router.post("/register",async (ctx) => {
    let bodys = ctx.request.body
    let {username,password} = bodys // 前台传递过来的用户名和密码
    let sql = `select * from users where username='${username}'`
    let {rows} = await db.query(sql)
    console.log(158,rows)
    if(rows.length>0) {
        ctx.body = {
            status:200,
            message:"该用户名已存在"
        }
    }else {
        let sql = `insert into users(username,password) values('${username}','${password}')`
        await db.query(sql)
        ctx.body = {
            status:200,
            message:"注册成功"
        }
    }
})
module.exports = router